package com.jf.socketMess;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import com.jf.demo.R;
import com.sys.android.entity.Msg;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ChatMsgViewAdapter extends BaseAdapter {
	private Context cxt;
	private LayoutInflater inflater;
	private List<Msg> listMsg;
	private MediaPlayer mMediaPlayer = new MediaPlayer();

	public ChatMsgViewAdapter(Context formClient, List<Msg> list) {
		this.cxt = formClient;
		listMsg = list;
		this.inflater  = LayoutInflater.from(formClient);
	}

	@Override
	public int getCount() {
		return listMsg.size();
	}

	@Override
	public Object getItem(int position) {
		return listMsg.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if ("IN".equals(listMsg.get(position).getFrom())) {
			convertView = this.inflater.inflate(
					R.layout.chatting_item_msg_text_left, null);
		} else {
			convertView = this.inflater.inflate(
					R.layout.chatting_item_msg_text_right, null);
		}
		TextView dateView = (TextView) convertView
				.findViewById(R.id.tv_sendtime);

//		TextView useridView = (TextView) convertView
//				.findViewById(R.id.tv_username);
		TextView msgView = (TextView) convertView
				.findViewById(R.id.tv_chatcontent);
		// useridView.setText(listMsg.get(position).getUserid());
		dateView.setText(listMsg.get(position).getDate());
		msgView.setText(listMsg.get(position).getMsg());
		msgView.setClickable(true);
		if (!Msg.TYPE[2].equals(listMsg.get(position).getType())) {// normal
																	// 普通msg
			final Msg msg = listMsg.get(position);
			TextView msgStatus = (TextView) convertView
					.findViewById(R.id.tv_time);
			msgStatus.setText(listMsg.get(position).getReceive() + "");
			msgView.setOnClickListener(new OnClickListener() {// 点击查看
				@Override
				public void onClick(View v) {
					if (msg.getFilePath().contains(".amr")) {

						playMusic(msg.getFilePath());
					}
				}
			});
		} else {
			TextView msgStatus = (TextView) convertView
					.findViewById(R.id.tv_time);
			msgStatus.setVisibility(View.GONE);// 影藏
		}

		return convertView;
	}

	/**
	 * @Description
	 * @param name
	 */
	private void playMusic(String name) {
		try {
			File file = new File(name);
			FileInputStream fis = new FileInputStream(file);

			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.stop();
			}
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(fis.getFD());
			mMediaPlayer.prepare();
			mMediaPlayer.start();
			mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void stop() {

	}

}
